using System;
using System.Collections.Generic;
using System.Text;
using Impact.Model.Objects;

namespace Impact.Model.Service
{
    /// <summary>
    /// data service that provides tag data
    /// </summary>
    public class TagService : ITagService
    {
        #region ITagService Members

        /// <summary>
        /// retrieve a tag based on its ID
        /// </summary>
        public Tag FetchByID(int tagId)
        {
            return Tag.FetchByID(tagId);
        }

        /// <summary>
        /// retrieve a tag based on the text in the tag.  the input text must exactly match the tag's text
        /// </summary>
        public Tag FetchByText(string tagText)
        {
            Tag output = new Tag();
            output.LoadByParam(Tag.Columns.Text, tagText.Trim());
            return output;
        }

        /// <summary>
        /// retrieve all tags
        /// </summary>
        public TagCollection FetchAll()
        {
            return new TagCollection()
                .OrderByAsc(Tag.Columns.Text)
                .Load();
        }

        /// <summary>
        /// retrieve all ArticleTag relationships by tag
        /// </summary>
        /// <param name="tagId">the ID of the tag to get relationships for</param>
        public ArticleTagCollection FetchArticleTags(int tagId)
        {
            return new ArticleTagCollection()
                .Where(ArticleTag.Columns.TagId, tagId)
                .Load();
        }

        /// <summary>
        /// save the given tag
        /// </summary>
        public void Save(Tag tag)
        {
            tag.Save();
        }

        /// <summary>
        /// remove the given tag
        /// </summary>
        public void Remove(int tagId)
        {
            Tag.Delete(tagId);
        }


        #endregion
    }
}
